package com.example.test1.account;

import com.example.demo.model.Account;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class AccountRepository {
    private Map<Long, Account> accounts = new HashMap<>();

    public void save(Account account) {
        accounts.put(account.getId(), account);
    }

    public Optional<Account> findById(Long id) {
        return Optional.ofNullable(accounts.get(id));
    }

    // 更新余额的逻辑
    public boolean updateBalance(Long id, double newBalance) {
        if (!accounts.containsKey(id)) {
            return false;
        }

        Account account = accounts.get(id);
        account.setBalance(newBalance);

        return true;
    }

    public boolean hasSufficientBalance(Long id, double amount) {
        return findById(id).map(account -> account.getBalance() >= amount).orElse(false);
    }
}